//
//  ViewController.m
//  AndonImagePicker
//
//  Created by 董文博 on 15/1/7.
//  Copyright (c) 2015年 OA. All rights reserved.
//

#import "PickerViewController.h"
#import "AndonImagePickerController.h"
#import "AndonImageBrowserController.h"
#import "ImageCViewCell.h"


@interface PickerViewController ()<AndnonImagePickerControllerDelegate,UICollectionViewDataSource,UICollectionViewDelegate,UIImagePickerControllerDelegate>

@property(nonatomic,strong)NSMutableArray *images;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property(strong,nonatomic) AndonImagePickerController *aipc;

@end

@implementation PickerViewController

-(AndonImagePickerController *)aipc
{
    if (_aipc == nil) {
        _aipc = [[AndonImagePickerController alloc] init];
    }
    return _aipc;
}

-(NSMutableArray *)images
{
    if (_images == nil) {
        _images = [[NSMutableArray alloc] init];
    }
    return _images;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.maximumZoomScale = 2;
    [self.collectionView registerClass:[ImageCViewCell class] forCellWithReuseIdentifier:@"image"];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.images.count;
}
- (IBAction)cameraOnClick:(UIButton *)sender {
    UIImagePickerController *icc = [[UIImagePickerController alloc] init];
    icc.sourceType = UIImagePickerControllerSourceTypeCamera;
    icc.delegate = self;
    [self presentViewController:icc animated:YES completion:nil];
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *cameraImage = info[@"UIImagePickerControllerOriginalImage"];
    [self.images addObject:cameraImage];
    [self.collectionView reloadData];
    [picker dismissViewControllerAnimated:YES completion:^{
        
    }];
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    AndonImageBrowserController * aibc = [[AndonImageBrowserController alloc] init];
    aibc.images = self.images;
    aibc.didFinish = ^{[self.collectionView reloadData];};
    aibc.index = indexPath.row;
    aibc.isDelete = YES;
    [self.navigationController pushViewController:aibc animated:YES];
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ImageCViewCell *icvc = (ImageCViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"image" forIndexPath:indexPath];
    icvc.backgroundColor = [UIColor redColor];
    UIImage *image = self.images[indexPath.row];
    icvc.imageView.image = image;
    return icvc;
}

-(void)imagePickerController:(AndonImagePickerController *)picker didFinishPickingMediaWithAssets:(NSArray *)assets
{
    for (ALAsset *asset in assets) {
        ALAssetRepresentation* representation = [asset defaultRepresentation];
        UIImage *image = [UIImage imageWithCGImage:[representation fullScreenImage]];
        [self.images addObject:image];
    }
    [self.collectionView reloadData];
}

- (IBAction)photoOnClick:(UIButton *)sender {
    self.aipc.delegate = self;
    self.aipc.maxSelectNum = 4-self.images.count;
    [self presentViewController:self.aipc animated:YES completion:^{
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
